package com.iwhalecloud.bss.kite.cucc.service.customerOrderInformation;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.bss.hsf.baseinf.service.InfExchangeService;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.customerOrderInformation.IGroupMemberService;
import com.iwhalecloud.bss.kite.cucc.client.dto.cust.MemberInfoReq;
import com.iwhalecloud.bss.kite.cucc.client.vo.cust.MemberInfoVO;
import com.iwhalecloud.bss.kite.cucc.common.callclient.HttpsCallClient;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalCallClientEnum;
import com.iwhalecloud.bss.kite.cucc.service.util.LocalTokenUtil;
import com.ztesoft.bss.common.bo.UserInfo;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.common.util.SpringUtil;
import com.ztesoft.zsmart.core.log.ZSmartLogger;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Classname GroupMemberService
 * @Description 成员用户信息查询
 * @Author zhang.song
 * @Date 2021-04-19 20:34
 */
@Service
public class GroupMemberService implements IGroupMemberService {

    private static final ZSmartLogger LOGGER = ZSmartLogger.getLogger(GroupMemberService.class);

    @Value("${interface.call.mode:hsf}")
    private String mode;


    /**
     * 成员用户信息查询
     * @author zhang.song
     * @date 2021-04-28 13:15
     * @param memberInfoReq
     * @return com.iwhalecloud.bss.kite.cucc.client.vo.cust.MemberInfoVO
     */
    @Override
    public MemberInfoVO qryGrpMemberInfo(MemberInfoReq memberInfoReq) {
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("UNI_BSS_BODY", this.getReqParams(memberInfoReq));
        requestMap.put("OP_CODE", "QryGrpMemberInfo");
        String respStr;
        if (KiteStringUtils.isEqual(mode, "http")) {
            respStr = HttpsCallClient.postStringWithObject(LocalCallClientEnum.INF_COMMON_URL, requestMap, LocalTokenUtil.getHeaderMap());
        } else {
            InfExchangeService infExchangeService = SpringUtil.getBean("infExchangeService");
            respStr = infExchangeService.exchange(new JSONObject(requestMap));
        }

        Map respMap = JSON.parseObject(respStr, Map.class);
        if(!"0000".equals(MapUtils.getString(respMap, "RESP_CODE"))) {
            MemberInfoVO memberInfoVO = new MemberInfoVO();
            memberInfoVO.setCode(MapUtils.getString(respMap, "RESP_CODE"));
            memberInfoVO.setMessage(MapUtils.getString(respMap, "RESP_DESC"));
            return memberInfoVO;
        }

        Map<String, Object> memUserInfo = MapUtils.getMap(respMap, "memUserInfo");
        MemberInfoVO memberInfoVO = JSONObject.parseObject(JSONObject.toJSON(memUserInfo).toString(), MemberInfoVO.class);
        memberInfoVO.setCode("0000");
        return memberInfoVO;
    }

    private Map<String, Object> getReqParams(MemberInfoReq memberInfoReq) {
        Map<String, Object> uniBssBody = new HashMap<>();
        uniBssBody.put("ORIG_DOMAIN", "BBSS");
        uniBssBody.put("ACTION_ID", "100024");
        uniBssBody.put("CHANNEL_ID", "9999999");
        uniBssBody.put("CHANNEL_TYPE", "1000000");
        uniBssBody.put("OPER_ID", ContextUtil.getUserId());
        UserInfo userInfo = ContextUtil.getLoginInfo().getUserInfo();
        uniBssBody.put("PROVINCE_CODE", userInfo.getPostProvinceNbr());
        uniBssBody.put("EPARCHY_CODE", userInfo.getPostLanId());
        uniBssBody.put("CITY_CODE", userInfo.getPostLanId());

        Map<String, String> beanMap = null;
        try {
            beanMap = BeanUtils.describe(memberInfoReq);
        } catch (IllegalAccessException e) {
            LOGGER.error(e);
        } catch (InvocationTargetException e) {
            LOGGER.error(e);
        } catch (NoSuchMethodException e) {
            LOGGER.error(e);
        }
        if(beanMap != null && beanMap.size() > 0) {
            // 删除obj的原型class
            beanMap.remove("class");
            uniBssBody.putAll(beanMap);
        }
        return uniBssBody;
    }
}
